﻿using Ross.OA.Client.Dto;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ross.OA.Client
{
    public partial class FormRecivePoPlan : Form
    {
        private string _skinFile;
        public List<ReceiceDto> dataSource;
        public FormRecivePoPlan(string skinFile)
        {
            InitializeComponent();
            _skinFile = skinFile;
        }

        private void FormRecivePoPlan_Load(object sender, EventArgs e)
        {
            skinEngine1.SkinFile = _skinFile;
            this.splitContainer1.SplitterDistance = 380;
            SetData();
        }
        private void SetData()
        {
            if (dataSource.Count > 0)
            {
                factory_code.Text = dataSource[0].factory_code;
                supplier_code.Text = dataSource[0].supplier_code;
                fits_code.Text = dataSource[0].fits_code;
                plan_version.Text = dataSource[0].plan_version;
                plan_month.Text = dataSource[0].plan_month.ToString("yyyy-MM");
                batch_serial.Text = dataSource[0].batch_serial;
                plan_num.Text = dataSource[0].plan_num.ToString();
            }
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            DoSetData();
            foreach (var item in dataSource)
            {
                if (item.quantity_rate == null || item.quantity_rate.Length <= 0 || item.quantity_rate[0].fits_rate <= 0)
                {
                    Comms.Warn("下线合格率必须大于 0 小于等于 100！");
                    return;
                }
                if (item.stock != null && item.stock.Length > 0)
                {
                    if (item.stock[0].fits_stock < item.plan_num)
                    {
                        Comms.Warn("供应商成品库存不小于计划数量!");
                        return;
                    }
                }
                else
                {
                    if (item.semi_stock == null || item.semi_stock.Length <= 0)
                    {
                        Comms.Warn("供应商成品库存不小于计划数量或存在至少一条半成品库存!");
                        return;
                    }
                    else
                    {
                        if (item.semi_stock[0].supplier_fits_stock < item.plan_num)
                        {
                            Comms.Warn("供应商半成品库存不小于计划数量!");
                            return;
                        }
                    }
                }
                
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void buttonSet_Click(object sender, EventArgs e)
        {
            
        }
        private void DoSetData()
        {
            ReceiceDto dto = dataSource[0];

            dto.stock = new Stock[] { new Stock() {
                    stock_date = stock_stock_date.Value.ToString("yyyy-MM-dd"),
                    fits_stock = Comms.CInt(stock_fits_stock.Text)
                } };
            dto.semi_stock = new Semi_Stock[] { new Semi_Stock() {
                    supplier_fits_stock = Comms.CInt(semi_stock_fits_stock.Text),
                    stock_date = semi_stock_stock_date.Value.ToString("yyyy-MM-dd"),
                    supplier_fits_code = semi_stock_supplier_fits_code.Text,
                    supplier_fits_name = semi_stock_supplier_fits_name.Text
                } };
            dto.quantity_rate = new Quantity_Rate[] { new Quantity_Rate() {
                    quantity_month = quantity_rate_quantity_month.Value.ToString("yyyy-MM-dd"),
                    fits_quantity = Comms.CInt(quantity_rate_fits_quantity.Text),
                    fits_rate = Comms.CInt(quantity_rate_fits_rate.Text)
                } };
        }

        private void stock_fits_stock_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && e.KeyChar != (char)Keys.Back)
            {
                e.Handled = true;
            }
        }

        private void semi_stock_fits_stock_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && e.KeyChar != (char)Keys.Back)
            {
                e.Handled = true;
            }
        }

        private void quantity_rate_fits_quantity_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && e.KeyChar != (char)Keys.Back)
            {
                e.Handled = true;
            }
        }

        private void quantity_rate_fits_rate_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && e.KeyChar != (char)Keys.Back)
            {
                e.Handled = true;
            }
        }
    }
}
